    <script type="text/javascript">

        var lastResponse = {}
        var MyMech = {
        
            player:    "{%if mech.player = None %}{%else%}{{ mech.player|escapejs }}{%endif%}",
            speed:     "{%if mech.speed = None %}{%else%}{{ mech.speed|escapejs }}{%endif%}",
            dropid:    "{%if mech.dropid = None %}{%else%}{{ mech.dropid|escapejs }}{%endif%}",
            mechtype:  "{%if mech.mechtype = None %}{%else%}{{ mech.mechtype|escapejs }}{%endif%}",
            lance:     "{%if mech.lance = None %}{%else%}{{ mech.lance|escapejs }}{%endif%}",
            starter:   "{%if mech.starter = None %}{%else%}{{ mech.starter|escapejs }}{%endif%}",
            uav:       "{%if mech.uav = None %}{%else%}{{ mech.uav|escapejs }}{%endif%}",
            ecm:       "{%if mech.ecm = None %}{%else%}{{ mech.ecm|escapejs }}{%endif%}",
            bombs:     "{%if mech.ecm = None %}{%else%}{{ mech.bombs|escapejs }}{%endif%}",
            artillery: "{%if mech.ecm = None %}{%else%}{{ mech.artillery|escapejs }}{%endif%}",
            bap:       "{%if mech.bap = None %}{%else%}{{ mech.bap|escapejs }}{%endif%}",
            artemis:   "{%if mech.artemis = None %}{%else%}{{ mech.artemis|escapejs }}{%endif%}",
            ams:       "{%if mech.ams = None %}{%else%}{{ mech.ams|escapejs }}{%endif%}",
            other:     "{%if mech.other = None %}{%else%}{{ mech.other|escapejs }}{%endif%}",
            weapons:   [
            {% if mech.weapons = None %}{% else %}{% for weapon in mech.weapons %}
                "{{ weapon }}",{% endfor %}{% endif %}
            ]    
        }        
        

            
            

        $(".post").hide();
        $("#guide").show();
        $("#roster").show();
        $( "#menu" ).menu();
        $( "#menu2" ).menu();
        $( "#menu3" ).menu();
        $( "#menu4" ).menu();
        
        $( "ul.player" ).selectable();
        
        function jpost( params, done, fail)
        {
            if ( !params ) params = {};
            if ( !done ) done = function(data) {};
            if ( !fail ) fail = function(data) {};
            
            var payload = { 
                d:          MyMech.dropid, 
                p:          MyMech.player, 
                sp:         MyMech.speed,
                m:          MyMech.mechtype,
                w:          MyMech.weapons.join(","),
                ebombs:     MyMech.bombs,
                eartillery: MyMech.artillery,
                eartemis:   MyMech.artemis,
                eams:       MyMech.ams,
                eecm:       MyMech.ecm,
                ebap:       MyMech.bap,
                euav:       MyMech.uav,
                eother:     MyMech.other,
            };
            
            for (key in params)
                payload[key] = params[key];
                
            $.ajax({ 
                url: "/json",
                dataType: 'json',
                type: "POST",
                data: payload
            })
            .done( done ).fail( fail );
        }
        
        function post( params, done, fail)
        {
            if ( !params ) params = {};
            if ( !done ) done = function(data) {};
            if ( !fail ) fail = function(data) {};
                  
            var payload = { 
                d:          MyMech.dropid, 
                p:          MyMech.player, 
                sp:         MyMech.speed,
                m:          MyMech.mechtype,
                w:          MyMech.weapons.join(","),
                ebombs:     MyMech.bombs,
                eartillery: MyMech.artillery,
                eartemis:   MyMech.artemis,
                eams:       MyMech.ams,
                eecm:       MyMech.ecm,
                ebap:       MyMech.bap,
                euav:       MyMech.uav,
                eother:     MyMech.other,
            };
            
            for (key in params)
                payload[key] = params[key];
                
            $.ajax({ 
                url: "/",
                //dataType: 'text/html',
                type: "POST",
                data: payload
            })
            .done( done ).fail( fail );
        }
        
        function saveMech()
        {
            //alert("save");
            jpost(
                {}, 
                function(data)
                {
                    //alert("good");
                    lastResponse = data;
                    MyMech = data.mech;
                    UpdateUI();
                }, 
                function(data,x, y)
                {
                    alert("bad " + data);
                    alert("bad " + x);
                    alert("bad " + y);
                }
            );
        }
        
        function UpdateUI()
        {
            UpdateMech();
            UpdateMechList();
        }
        
        function UpdateMechList()
        {
            post({ e:"1" }, function (data) {
                $("#rosterdiv").html(data);
            });
        }
        
        function AutoUpdateMechList()
        {
            UpdateMechList();
            setTimeout( AutoUpdateMechList, 15000);
        }
        
        setTimeout( AutoUpdateMechList, 15000);
        
        function UpdateMech()
        {
            //alert("U" + $("#uimechname").text());
            if ( MyMech.mechtype != "" )
                // $("#uimechname").text(MyMech.mechtype);
                $("#uimechname, #uimechnameof{{ mech.player|escapejs }}").text(MyMech.mechtype);
            else
                $("#uimechname, #uimechnameof{{ mech.player|escapejs }}").text("'Mech not selected");
                
            if ( MyMech.speed > 0 )
                $("#uispeed, #uispeedof{{ mech.player|escapejs }}").html("&#8776;" + MyMech.speed + " km/h");
            else
                $("#uispeed, #uispeedof{{ mech.player|escapejs }}").text("Speed not selected");
            
            wa = [];
            wo = {};

            for (i in MyMech.weapons)
            {
                w = MyMech.weapons[i]

                if( wo[w] )
                {

                    wo[w]++;
                }
                else
                {
                    wa.push(w); 
                    wo[w] = 1;                    
                }
            }
            
            ww = [];
            
            for (i in wa)
            {
                w = wa[i]
                n = wo[w];
                
                if (n > 1)
                    ww.push(n + " " + w);
                else
                    ww.push(w);                    
            }
            
            if (MyMech.weapons.length > 0)
                $("#uiweapons, #uiweaponsof{{ mech.player|escapejs }}").html(ww.join(", "))
            else
                $("#uiweapons, #uiweaponsof{{ mech.player|escapejs }}").text("Weapons not selected");
            
            e = "";
            
            if ( MyMech.bap == "1" ) e = e + "+BAP "
            if ( MyMech.ecm  == "1" ) e = e + "+ECM "
            if ( MyMech.uav  == "1" ) e = e + "+UAV "
            if ( MyMech.ams  == "1" ) e = e + "+AMS "
            if ( MyMech.artemis  == "1" ) e = e + "+Artemis "
            if ( MyMech.bombs  == "1" ) e = e + "+Airstrike "
            if ( MyMech.artillery  == "1" ) e = e + "+Artillery "
            if ( MyMech.other  == "1" ) e = e + "+Other "

            if ( e != "" )
                $("#uiequipment, #uiequipmentof{{ mech.player|escapejs }}").text(e);
            else
                $("#uiequipment, #uiequipmentof{{ mech.player|escapejs }}").text("Equipment not selected");

            
            //alert("U2" + $("#uimechname").text());
        }

        weapons = ["slsr", "splsr", "mlsr", "mplsr", "llsr", "lplsr", "ppc", "erppc", "tag", "flamer", "mg", "ac2", "ac5", "ac5u", "ac10", "ac10x", "ac20", "gauss", "srm2", "srm2s", "srm4", "srm6", "lrm5", "lrm10", "lrm15", "lrm20", "narc"] 
        
        for (i in weapons)
        {
            $("#add_weapon_" + weapons[i]).click(function(caller, x) { 
                MyMech.weapons.push( $(this).html() );
                saveMech()
            });
        }
        
        {% for class, mlist in tonnages_grouped.items %}
        {% for model, vlist in mlist.items %}
        {% for variant in vlist  %}
        $("#selectmech_{{model|escapejs}}{{variant|escapejs}}").click(function(caller, x) { 
            mech = "{{model|escapejs}}-{{variant|escapejs}}".replace("HERO-", "");
            MyMech.mechtype = mech;
            saveMech()
        });
        {% endfor %}
        {% endfor %}
        {% endfor %}
        
        
        
        {% for s in speeds %}
        $("#setspeed_{{s}}").click(function(caller, x) { 
            MyMech.speed = "{{ s }}"
            saveMech()
        });
        {% endfor %}
        
        $("#estrip").click(function(caller, x) { 
            MyMech.uav = 0;
            MyMech.bap = 0;
            MyMech.ecm = 0;
            MyMech.bombs = 0;
            MyMech.artillery = 0;
            MyMech.artemis = 0;
            MyMech.ams = 0;
            MyMech.other = 0;
            saveMech();
        });        
        
        $("#wstrip").click(function(caller, x) { 
            MyMech.weapons = [];
            saveMech();
        });
        
        $("#leavedrop").click(function(caller, x) {
            jpost( 
                { players: MyMech.player, a: "x" }, 
                function(data) {
                    //location.href='/';
                }
            ); 
        })
        
        $("#takelead").click(function(caller, x) {
            jpost( 
                { players: MyMech.player, a: "l" }, 
                function(data) {
                    lastResponse = data;
                    MyMech = data.mech;
                    UpdateUI();
                }
            ); 
        });
    
        $("#uisavemechd").button();
        $("#uicancelsavemechd").button();
        $("#uisavemechd").click(
            function () { 
                alert("S");
                jpost( 
                    { 
                        n: $("#uiinputname").val(), 
                    }, 
                    function(data) {
                        alert("D");
                        MyMech.name = $("#uiinputname").val();
                        $( "#dialog-modal" ).dialog("close");
                    }, 
                    function(data) {
                        alert("F");
                        $( "#dialog-modal" ).dialog("close");
                    }
                );
                $( "#dialog-modal" ).dialog("close");
            }
        );
        $("#uicancelsavemechd").click(
            function () { 
                $( "#dialog-modal" ).dialog("close");                
            }
        )
        
        $("#uisave").click(function(caller, x) {
            $("#uiinputname").val( MyMech.name );
            $( "#dialog-modal" ).dialog({
                height: 180,
                modal: true,
                
            });
             
        });
            

        $(".equipment").click(function(caller, x) {
        
            var link = $(this).text();
            // alert(link);
            if ( link == "ECM" )
                MyMech.ecm = 1;
            else if ( link == "UAV" )
                MyMech.uav= 1;
            else if ( link == "BAP" )
                MyMech.bap= 1;
            else if ( link == "Artemis" )
                MyMech.artemis= 1;
            else if ( link == "AMS" )
                MyMech.ams = 1;
            else if ( link == "Artillery" )
                MyMech.artillery = 1;
            else if ( link == "Airstrike" )
                MyMech.bombs = 1;
            else if ( link == "Other" )
                MyMech.other = 1;
            
            saveMech();
        });
        
        $(".tool").click(function(caller, x) {
        
            $(".post").hide();
  
            var link = $(this).attr( "href" );
 
            if ( link !="#overview" && link !="#download" && link !="#faq")
                $("#roster").show();
                
            $(link).show();
  
            return false;
        });
        
         $.ajax({ 
            url: "/garage",
            type: "POST",
            data: { p: MyMech.player },
        }).done( function(data) {
             $("#uigaragelist").html(data);
            $("#uigaragelist").menu("refresh");
        });
       


    </script>